﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WF.Web.Pages.WorkflowInfo
{
    public partial class FlowTypeList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string procInstID = Request.QueryString.Get("ProcInstID");
            CreateTree(procInstID);
            LocateNode(tvFlowType.Nodes[0], "Static");
        }

        private void CreateTree(string procInstID)
        {
            this.tvFlowType.Nodes.Clear();

            TreeNode nodeRoot = new TreeNode();
            nodeRoot.Text = "请选择";
            nodeRoot.Value = "Select";
            nodeRoot.Expanded = true;
            nodeRoot.NavigateUrl = "";
            nodeRoot.SelectAction = TreeNodeSelectAction.None;
            this.tvFlowType.Nodes.Add(nodeRoot);

            TreeNode subStaticNode = new TreeNode();
            subStaticNode.Text = "静态图形";
            subStaticNode.Value = "Static";
            subStaticNode.ToolTip = "以图片格式显示流程图";
            subStaticNode.NavigateUrl = "ViewFlow.aspx?ProcInstID=" + procInstID;
            nodeRoot.ChildNodes.Add(subStaticNode);

            TreeNode subDynamicNode = new TreeNode();
            subDynamicNode.Text = "动态图形";
            subDynamicNode.Value = "Dynamic";
            subDynamicNode.ToolTip = "可以直观形象地反映流程的状态";
            subDynamicNode.NavigateUrl = System.Configuration.ConfigurationManager.AppSettings["K2ViewWorkFlowURL"] + "&ProcessID=" + procInstID;
            nodeRoot.ChildNodes.Add(subDynamicNode);
        }

        #region 定位到与某个值匹配的节点，并且选中
        private bool LocateNode(TreeNode tnode, string keyValue)
        {
            bool founded = false;
            if (tnode.ChildNodes.Count == 0)
                return false;
            tnode.Expand();
            foreach (TreeNode tn in tnode.ChildNodes)
            {
                if (tn.Value.ToLower() == keyValue.ToLower())
                {
                    tn.Selected = true;
                    tn.SelectAction = TreeNodeSelectAction.Select;
                    tn.Expand();
                    founded = true;
                    break;
                }
                if (LocateNode(tn, keyValue)) { founded = true; break; }
            }
            if (!founded)
            {
                tnode.Collapse();
            }
            return founded;
        }
        #endregion 定位到与某个值匹配的节点，并且选中
    }
}